//
//  WSCollectionManager.m
//  Component
//
//  Created by 王诗文 on 2018/7/26.
//  Copyright © 2018年 王诗文. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "UICollectionView+WSExtension.h"
#import "UICollectionReusableView+WSExtension.h"
#import "WSCollectionManager.h"
#import "WSListPlaceHolderView.h"
#import "WSModule.h"
#import "WSCollectionCellProtocol.h"

@interface WSCollectionManager()<WSCollectionReusableViewActionDelegate,WSListPlaceHolderViewDelegate,WSCollectCellSubViewsActionDelegate>
@property(nonatomic, strong) WSListPlaceHolderView *holderView;
@property(assign, nonatomic) BOOL isDragging;

@end

@implementation WSCollectionManager


- (BOOL)wsShowHolder{
     if (self.modules.count==0&&(self.listStatus==WSListStatusNone||self.listStatus==WSListStatusFails||self.listStatus==WSListStatusLogin)) {
          return true;
     }
     return false;
}

#pragma mark -- UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
     if ([self wsShowHolder]) {
          self.holderView.hidden = false;
          self.collectionView.hidden = true;
          return 0;
     }
     self.holderView.hidden = true;
     self.collectionView.hidden = false;
     self.isDragging = NO;
    return self.modules.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    WSModule *module = self.modules[section];
    return module.datas.count;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    WSModule *module = self.modules[section];
    if (module.headerName==nil) return CGSizeZero;
    Class <WSCollectionCellProtocol>cls = NSClassFromString(module.headerName);
    if ([cls respondsToSelector:@selector(wsCalculateSizeWithContent:)]) {
        return [cls wsCalculateSizeWithContent:module];
    }
    return CGSizeZero;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    WSModule *module = self.modules[section];
    if (module.footerName==nil) return CGSizeZero;
    Class <WSCollectionCellProtocol>cls = NSClassFromString(module.footerName);
    if ([cls respondsToSelector:@selector(wsCalculateSizeWithContent:)]) {
        return [cls wsCalculateSizeWithContent:module];
    }
    return CGSizeZero;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    WSModule *module = self.modules[indexPath.section];
    Class <WSCollectionCellProtocol>cls = NSClassFromString(module.cellName);
    if ([cls respondsToSelector:@selector(wsCalculateSizeWithContent:)]) {
        return [cls wsCalculateSizeWithContent:module.datas[indexPath.row]];
    }
    return CGSizeZero;
}

//初始化headerview
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    WSModule *module = self.modules[indexPath.section];
    UICollectionReusableView <WSCollectionCellProtocol> *reuseView = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:module.headerName forIndexPath:indexPath];
         if (reuseView && [reuseView respondsToSelector:@selector(wsFillContent:)]) {
              [reuseView wsFillContent:module.headerData];
         }
    }else{
        reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:module.footerName forIndexPath:indexPath];
         if (reuseView && [reuseView respondsToSelector:@selector(wsFillContent:)]) {
              [reuseView wsFillContent:module.footerData];
         }
    }
     reuseView.wsDelegate = self;
     reuseView.section = indexPath.section;
    return reuseView;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WSModule *module = self.modules[indexPath.section];
    NSAssert(module.cellName!=nil, @"cellname must not nil");
    UICollectionViewCell <WSCollectionCellProtocol>*cell = [collectionView dequeueReusableCellWithReuseIdentifier:module.cellName forIndexPath:indexPath];
     cell.attachObj = self.attachObj;
    if ([cell respondsToSelector:@selector(wsFillContent:)]) {
        [cell wsFillContent:module.datas[indexPath.row]];
    }
     cell.wsDelegate = self;
     cell.indexPath = indexPath;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.cellClickedBlock) {
         WSModule *module = self.modules[indexPath.section];
         self.cellClickedBlock(indexPath,module.datas[indexPath.row]);
    }
}


//header footer 点击事件代理方法
- (void)headerClickedInSection:(NSInteger)section data:(id)data{
    if (self.headerClickedBlock) {
        self.headerClickedBlock(section,data);
    }
}

- (void)footerClickedInSection:(NSInteger)section data:(id)data{
    if (self.footerClickedBlock) {
        self.footerClickedBlock(section,data);
    }
}

- (void)holderButtonClickWithStatus:(WSListStatus)status{
     if (self.placeHolderBlock) {
          self.placeHolderBlock(status);
     }
}


//cell 内部点击事件
- (void)wsCellSubViewsClickedIndexPath:(NSIndexPath *)indexPath data:(id)data{
     if (self.cellsubViewsClickedBlock) {
          self.cellsubViewsClickedBlock(indexPath, data);
     }
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
     WSModule *module = self.modules[section];
     Class <WSCollectionCellProtocol>cls = NSClassFromString(module.cellName);
     if ([cls respondsToSelector:@selector(wsEdgeInsets)]) {
          return [cls wsEdgeInsets];
     }
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
     WSModule *module = self.modules[section];
     Class <WSCollectionCellProtocol>cls = NSClassFromString(module.cellName);
     if ([cls respondsToSelector:@selector(wsMinimumLineSpacing)]) {
          return [cls wsMinimumLineSpacing];
     }
     return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
     WSModule *module = self.modules[section];
     Class <WSCollectionCellProtocol>cls = NSClassFromString(module.cellName);
     if ([cls respondsToSelector:@selector(wsMinimumInteritemSpacing)]) {
          return [cls wsMinimumInteritemSpacing];
     }
     return 0;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
    if (@available(iOS 11.0, *)) {
        if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {
            view.layer.zPosition = 0;
        }
    }
}



- (void)registerCells{
    if (_modules&&_collectionView){
        [_modules enumerateObjectsUsingBlock:^(WSModule * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.collectionView wsRegisterCellName:obj.cellName];
            [self.collectionView wsRegisterHeaderName:obj.headerName];
            [self.collectionView wsRegisterFooterName:obj.footerName];
        }];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
     self.isDragging = YES;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
     if (self.scrollViewDidScrollBlock && self.isDragging) {
          self.scrollViewDidScrollBlock(scrollView);
     }
}

#pragma mark - set\get

- (void)setModules:(NSArray<WSModule *> *)modules{
    _modules = modules;
    [self registerCells];
}

- (void)setCollectionView:(UICollectionView *)collectionView{
    _collectionView = collectionView;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self registerCells];
}

- (void)setListStatus:(WSListStatus)listStatus
{
     _listStatus = listStatus;
     self.holderView.listStatus = listStatus;
}
- (WSListPlaceHolderView *)holderView{
     if (_holderView==nil) {
          _holderView = [[WSListPlaceHolderView alloc] initWithFrame:self.collectionView.frame];
          _holderView.delegate = self;
          _holderView.hidden = true;
          [self.collectionView.superview addSubview:_holderView];
     }
     return _holderView;
}

@end
